package com.likerSai;

import java.util.*;

/**
 * 电话号码的字母组合
 * so easy
 * @author sun.ye
 * @since 2021-07-19 18:00
 */
public class LetterCombinations {
    private static final Map<String, List<String>> dictionary = new HashMap<>();
    static {
        dictionary.put("2",new ArrayList<>(Arrays.asList("a,b,c".split(","))));
        dictionary.put("3",new ArrayList<>(Arrays.asList("d,e,f".split(","))));
        dictionary.put("4",new ArrayList<>(Arrays.asList("g,h,i".split(","))));
        dictionary.put("5",new ArrayList<>(Arrays.asList("j,k,l".split(","))));
        dictionary.put("6",new ArrayList<>(Arrays.asList("m,n,o".split(","))));
        dictionary.put("7",new ArrayList<>(Arrays.asList("p,q,r,s".split(","))));
        dictionary.put("8",new ArrayList<>(Arrays.asList("t,u,v".split(","))));
        dictionary.put("9",new ArrayList<>(Arrays.asList("w,x,y,z".split(","))));
    }

    public static void main(String[] args) {
        List<String> result = getCombinations("234");
        System.out.println(result);
        System.out.println(result.size());
    }

    public static List<String> getCombinations(String digits){
        List<String> resultOut = new ArrayList<>();
        if (digits.length() == 0 ){
            return resultOut;
        }else if (digits.length() == 1){
            return dictionary.get(digits);
        }else {
            String headStr = digits.substring(0,1);
            List<String> subResult = getCombinations(digits.substring(1));
            for (String itemHead : dictionary.get(headStr)){
                for (String itemSub : subResult){
                    resultOut.add(itemHead + itemSub);
                }
            }
            return resultOut;
        }
    }

}